package com.zzw.basic.io.nio;

import org.junit.Test;

import java.nio.ByteBuffer;

/**
 * @author zzw
 * @see
 * @since 2018/2/22
 */
public class ByteBufferTest {

    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byte[] temp = new byte[]{1,2,3,4,5,6};
        byteBuffer.put((byte) 1);
        byteBuffer.put((byte) 2);
        byteBuffer.put((byte) 3);
        byteBuffer.put((byte) 4);
        byteBuffer.put((byte) 5);
        byteBuffer.put((byte) 6);
        //byteBuffer.put(temp);
        System.out.println("position:" + byteBuffer.position());
        System.out.println("limit:" + byteBuffer.limit());
        byteBuffer.flip();
        byteBuffer.put((byte) 123);
        byteBuffer.put((byte) 124);
        System.out.println("position:" + byteBuffer.position());
        System.out.println(byteBuffer.get(0));
        System.out.println(byteBuffer.get(1));
        System.out.println("limit:" + byteBuffer.limit());
    }

    @Test
    public void test1(){
        ByteBuffer bBuf = ByteBuffer.allocate(512);
        bBuf.putInt(1);
        bBuf.putInt(2);
        bBuf.putInt(3);
        bBuf.putInt(4);
        bBuf.putInt(5);
        bBuf.putInt(6);
        bBuf.putInt(7);
        bBuf.flip();
        System.out.println("缓冲区Pos：" + bBuf.position() + "  缓冲区Limit："+ bBuf.limit());
        bBuf.putInt(8);
        bBuf.putInt(9);
        //bBuf.get();
        System.out.println("缓冲区Pos：" + bBuf.position() + "  缓冲区Limit："+ bBuf.limit());
        System.out.println(bBuf.getInt());
        System.out.println(bBuf.getInt());
        System.out.println(bBuf.getInt());
        System.out.println(bBuf.getInt());
        System.out.println(bBuf.getInt());
    }

    @Test
    public void test2(){
        ByteBuffer bBuf = ByteBuffer.allocate(512);
        bBuf.putInt(1);
        bBuf.putInt(2);
        bBuf.putInt(3);
        bBuf.putInt(4);
        bBuf.putInt(5);
        bBuf.putInt(6);
        bBuf.putInt(7);
        bBuf.flip();
        System.out.println("缓冲区Pos：" + bBuf.position() + "  缓冲区Limit：" + bBuf.limit());
        for (int i = 0; i < 28; i = i+ 4) {
            System.out.println(bBuf.getInt(i));
        }
    }
}
